package com.rs.service.impl.rs;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.rs.dao.ProgressMapper;
import com.rs.dao.ProjectMapper;
import com.rs.model.CommonResult;
import com.rs.model.User;
import com.rs.model.Progress;
import com.rs.model.Project;
import com.rs.model.Relation;
import com.rs.service.rs.ProgressService;
import com.rs.util.TimeTypeConversionUtil;

@Service
public class ProgressServiceImpl implements ProgressService {
	@Autowired
	private ProgressMapper pm;
	@Autowired
	private ProjectMapper projectMapperm;

	/**
	 * 修改项目进度
	 */
	@Override
	@Transactional
	public CommonResult<String> progressUpdateApp(Progress progress) {
		//先查询relation，获取rel_id
		Relation relation = pm.selectRelation(progress);
		boolean flag = Optional.ofNullable(relation).isPresent();
		if (!flag) {
			return new CommonResult<String>(200, "未查到修改人");
		}
		if (progress.getPre_schedule() > 100) {
			return new CommonResult<String>(200, "项目进度不能大于100");
		}

		//修改进度表
		progress.setPre_time(new Date());
		progress.setPre_reid(relation.getRel_id());
		int count = pm.progressUpdateApp(progress);

		if (count != 0) {//修改进度表成功

			//修改项目的总进度
			//查询该项目中所有人的进度
			List<Progress> list = pm.selectOneProjectProgress(relation.getRel_prid());
			double maxdou = 0;//项目总进度
			for (Progress pp : list) {

				//目标进度
				double goal = Double.valueOf(pp.getPre_totalsche()) / 100;

				//实际个人进度
				double reality = Double.valueOf(pp.getPre_schedule()) / 100;

				//实际项目进度
				double proreal = goal * reality;

				maxdou = maxdou + proreal;
			}

			int maxint = new Double(maxdou * 100).intValue();//强转int

			//修改项目的总进度
			Project project = new Project();
			project.setPr_id(relation.getRel_prid());
			project.setPr_schedule(maxint);
			projectMapperm.projectUpdateProjectApp(project);
			return new CommonResult<String>(200, "修改成功");

		} else {
			return new CommonResult<String>(200, "修改失败");
		}

	}

	/**
	 * 查询该项目下的所有人员进度
	 */

	@Override
	public CommonResult<List<Progress>> progressQueryProjectAllApp(Integer pre_prid) {
		List<Progress> list = pm.selectOneProjectProgress(pre_prid);
		return new CommonResult<List<Progress>>(200, "查询成功", list);
	}

	/**
	 * 查询进度的详细信息
	 */
	@Override
	public CommonResult<Progress> progressQueryDetailApp(Integer pre_id) {
		Progress progress = pm.progressQueryDetailApp(pre_id);

		return new CommonResult<Progress>(200, "查询成功", progress);
	}

	/**
	 * 查询自己的进度列表
	 */
	@Override
	public CommonResult<List<Progress>> progressOwnDetailProApp(Integer pre_uid) {
		List<Progress> list = pm.progressOwnDetailProApp(pre_uid);
		return new CommonResult<List<Progress>>(200, "查询成功", list);
	}

	/**
	 * PC查询该项目下的所有人员进度
	 */
	@Override
	public CommonResult<Map<String, Object>> progressQueryProjectAllPc(Progress progress) {
		Page<Progress> page = PageHelper.startPage(progress.getPageNum(), 10);
		List<Progress> list = pm.selectOneProjectProgress(progress.getPre_prid());
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("list", list);
		map.put("total", page.getTotal());
		map.put("pageNum", page.getPageNum());
		map.put("pages", page.getPages());
		return new CommonResult<Map<String, Object>>(200, "查询成功", map);
	}

	/**
	 * pc查询所有的进度
	 */
	@Override
	public CommonResult<Map<String, Object>> progressQueryAllPc(Integer pageNum) {
		Page<Progress> page = PageHelper.startPage(pageNum, 10);
		List<Progress> list = pm.progressQueryAllPc();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("list", list);
		map.put("total", page.getTotal());
		return new CommonResult<Map<String, Object>>(200, "查询成功", map);
	}

	/**
	 * pc查询进度详情
	 */
	@Override
	public CommonResult<Progress> progressQueryDetailsPc(Integer pre_id) {
		Progress progress = pm.progressQueryDetailApp(pre_id);
		return new CommonResult<Progress>(200, "查询成功", progress);
	}

	/**
	 * pc条件查询
	 */
	@Override
	public CommonResult<Map<String, Object>> progressConditionQueryPc(Progress progress) {
		Page<Progress> page = PageHelper.startPage(progress.getPageNum(), 10);
		List<Progress> list = pm.progressConditionQueryPc(progress);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("list", list);
		map.put("total", page.getTotal());
		return new CommonResult<Map<String, Object>>(200, "查询成功", map);
	}

	/**
	 * pc根据时间查询
	 */
	@Override
	public CommonResult<Map<String, Object>> progressSetUpTimeQueryPc(Map<String, String> map) {
		Integer pageNum = Integer.valueOf(map.get("pageNum"));//当前页

		Date startTime = TimeTypeConversionUtil.getDateByString(map.get("startTime"));//开始时间
		Date endTime = TimeTypeConversionUtil.getDateByString(map.get("endTime"));//结束时间

		Page<User> page = PageHelper.startPage(pageNum, 10);//分页
		List<User> list = pm.progressSetUpTimeQueryPc(startTime, endTime);//list
		//map
		Map<String, Object> mm = new HashMap<String, Object>();
		mm.put("list", list);
		mm.put("total", page.getTotal());
		return new CommonResult<Map<String, Object>>(200, "查询成功", mm);
	}

}
